<?php

namespace App\Models\Kms\Attendance;

use App\Models\BaseModel;
use App\Models\Kms\User\KmsUser;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class AttendanceDailyGatherModel extends BaseModel
{
    protected $table = "attendance_daily_gather";

    protected $fillable = ["userId", "date", "createdByUid", "updatedByUid", "groupGatherId", "checkStatus"];

    const STATUS_NORMAL   = 1;
    const STATUS_ERROR    = 0;
    const STATUS_NO_TRACE = -1;
    const STATUS_TEXT     = [
        self::STATUS_NORMAL   => "轨迹正常",
        self::STATUS_ERROR    => "轨迹异常",
        self::STATUS_NO_TRACE => "无轨迹"
    ];

    const CHECK_STATUS_STAY   = 0;
    const CHECK_STATUS_FINISH = 1;

    /**
     * @return HasOne
     */
    public function lastDailyPointTraceRel(): HasOne
    {
        return $this->hasOne(AttendanceDailyPointTraceModel::class, "dailyGatherId", "id")
                    ->where("status", ">", AttendanceDailyPointTraceModel::STATUS_0)
                    ->orderByDesc("tracePointTime");
    }

    /**
     * @return HasMany
     */
    public function dailyPointTraceRel(): HasMany
    {
        return $this->hasMany(AttendanceDailyPointTraceModel::class, "dailyGatherId", "id");
    }

    /**
     * @return HasOne
     */
    public function userRel(): HasOne
    {
        return $this->hasOne(KmsUser::class, "rmsUserId", "userId");
    }
}
